public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;


        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }


    private Node[]  array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小

    public void put(int key, int value) {
        int index = key % array.length;
        if(array[index] == null) {
            array[index] = new Node(key, value);
        } else {
            Node node = new Node(key, value);
            putBack(array[index],node);
        }
        size++;
        if(loadFactor() >= LOAD_FACTOR) {
            resize();
        }
    }

    private void putBack(Node root, Node node) {
        Node cur = root;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }


    private void resize() {
        // write code here
        Node[] newarray = new Node[2 * array.length];
        for(Node x : array) {
            Node cur = x;
            while(cur != null) {
                Node prev = cur.next;
                int index = cur.key % newarray.length;
                if(newarray[index] == null) {
                    newarray[index] = cur;
                } else {
                    putBack(newarray[index],cur);
                }
                cur.next = null;
                cur = prev;
            }
        }
        array = newarray;
    }


    private double loadFactor() {
        return size * 1.0 / array.length;
    }


    public HashBucket() {
        // write code here
        array = new Node[DEFAULT_SIZE];
    }

    public int getArrayLenght() {
        return array.length;
    }

    public int get(int key) {
        // write code here
        int index = key / array.length;
        Node cur = array[index];
        while(cur != null) {
            if(cur.key == key) {
                return key;
            }
        }
        return -1;
    }
}
